<?php
/**
 * RoomService
 *
 * PHP Version 5.4
 *
 * @author liuxu <liuxuphp@icesmart.cn>
 */
namespace Admin\Service;
use Think\Controller;

/**
 * Class RoomService
 */
class RoomService extends Controller{

    /**
     * 获取直播间人数
     */
    public function getRoomNums()
    {
        $mem = new \Memcached("");
        $mem->addServer(C('MEMCACHE_HOST'), C('MEMCACHE_PORT'));

        $rooms = $mem->get('socket[SOCKET_SERVER_socket.rooms][1]');
        $roomList = [];
        $i = 0;
        foreach($rooms as $room){
            $users = $mem->get('socket[SOCKET_SERVER_socket.users'.$room.'][1]');
            $online = $mem->get('socket[SOCKET_SERVER_socket.online'.$room.'][1]');

            if(empty($users) && empty($online)){
                $nums = 0;
            }
            elseif(empty($online) && !empty($users)){
                $nums = count($users);
            }
            elseif(!empty($online) && empty($users)){
                $nums = count($online);
            }else{
                $nums = count($users)+count($online);
            }
            $roomList[$i]['roomId'] = $room;
            $roomList[$i]['roomNums'] = $nums;
            $i++;
        }
        return $roomList;
    }
}